from src.application.order.dto.order import OrderDTO
from src.domain.order.aggregates import OrderAggregate
from src.domain.order.value_objects import OrderId
from src.domain.user.value_objects import UserId
from src.infrastructure.db import models
from src.infrastructure.db.converters.allowed_ip import convert_allowed_ip_entity_to_db_model
from src.infrastructure.db.converters.proxy import convert_proxy_entity_to_db_model


def convert_db_model_to_order_dto(order: models.Order) -> OrderDTO:
    return OrderDTO(
        id=order.id,
        buyer_id=order.user_id,
        summ=order.summ,
        summ_total=order.summ_total,
        order_type=order.type,
        category_id=order.category_id,
        period=order.period,
        proxies=order.proxies,
        provider_id=order.provider_id,
        site_id=order.site_id,
        was_created=order.was_created,
    )


def convert_order_entity_to_db_model(order: OrderAggregate) -> models.Order:
    proxies = [convert_proxy_entity_to_db_model(proxy) for proxy in order.proxies]
    allowed_ips = [convert_allowed_ip_entity_to_db_model(allowed_ip) for allowed_ip in order.allowed_ips]

    return models.Order(
        id=order.id.value,
        user_id=order.buyer_id.value,
        proxies=proxies,
        allowed_ips=allowed_ips,
    )


def convert_db_model_to_order_entity(order: models.Order) -> OrderAggregate:
    return OrderAggregate(
        id=OrderId(order.id),
        buyer_id=UserId(order.user_id),
        summ=order.summ,
        summ_total=order.summ_total,
        order_type=order.type,
        category_id=order.category_id,
        period=order.period,
        proxies=order.proxies,
        country_id=order.country_id,
        provider_id=order.provider_id,
        site_id=order.site_id,
    )
